package net.costsoft.web.px.purchase.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.common.JsonResult;
import net.costsoft.web.px.purchase.vo.Purchaserequest;
import net.costsoft.web.px.purchase.service.PurchaserequestService;
 
/**
 * 
 * <br>
 * <b>功能：</b>PurchaserequestController<br>
 * <b>作者：</b>suxy<br>
 * <b>日期：</b>2018-7-27 16:14:46 <br>
 * <b>版权所有：
 */ 
@Controller
@RequestMapping("/purchaserequest") 
public class PurchaserequestController {
	
	private final static Logger log= LoggerFactory.getLogger(PurchaserequestController.class);
	
	@Autowired
	private PurchaserequestService purchaserequestService; 
	
	/**
	 * 列表
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(ModelMap model,Purchaserequest purchaserequest) throws Exception{
		List<Purchaserequest> dataList = purchaserequestService.query(purchaserequest);
		Integer total = purchaserequestService.queryPurchaserequestCount(purchaserequest);
		model.put("Purchaserequests", dataList);
		purchaserequest.setTotal(total);
		model.put("purchaserequest", purchaserequest);
		return new ModelAndView("list_Purchaserequest_view");
	}
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public Object save(ModelMap model,Purchaserequest entity) throws Exception{
			if(entity.getR_id()==null){
				purchaserequestService.insert(entity);
			}else{
				purchaserequestService.update(entity);
			}
		return new ModelAndView(new RedirectView("list.htm"));
	}
	
	@RequestMapping("/getId")
	public Object getId(ModelMap model,Purchaserequest purchaserequest) throws Exception{
		Purchaserequest entity  = purchaserequestService.queryById(purchaserequest);
		model.put("Purchaserequest", entity);
		if("e".equals(purchaserequest.getA())){
			return new ModelAndView("edit_Purchaserequest_view");
		}
		return new ModelAndView("view_Purchaserequest_view");
	}
	
	
	
	@RequestMapping("/delete")
	public Object delete(String id) throws Exception{
		purchaserequestService.delete(id);
		return new JsonResult("删除成功");
	}

	@InitBinder  
   protected void initBinder(WebDataBinder binder) {  
		 binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));  
   } 
   
}
